package com.lzc.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.lzc.dao.UserDao;
import com.lzc.pojo.User;
import com.mysql.jdbc.Driver;
/**
 * 第三步dao层的实现类
 * 实现数据库连接操作
 * @author lzc
 *
 */
public class UserDaoImpl implements UserDao {

	/**
	 * 登录模块    根据用户名密码查询用户信息
	 */
	@Override
	public User checkLoginDao(String uname, String pwd) {
		//声明JDBC对象
		Connection conn = null;//数据库驱动
		PreparedStatement ps = null;//sql语句
		ResultSet rs = null;//存储数据库返回
		//声明变量
		User u = null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/2020?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "19971003qaz");
			String sql = "select * from t_user where uname=? and pwd=? and flag=0";
			ps = conn.prepareStatement(sql);
			ps.setString(1, uname);
			ps.setString(2, pwd);
			rs = ps.executeQuery();
			while (rs.next()) {
				u = new User();
				u.setUid(rs.getInt("uid"));
				u.setUname(rs.getString("uname"));
				u.setPwd(rs.getString("pwd"));
				u.setSex(rs.getString("sex"));
				u.setAge(rs.getInt("age"));
				u.setBirth(rs.getString("birth"));
				u.setFlag(rs.getInt("flag"));
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return u;
	}

	@Override
	public int userRegDao(User u) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement ps = null;
		int index = -1;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/2020?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "19971003qaz");
			String sql = "insert into t_user values(default,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, u.getUname());
			ps.setString(2, u.getPwd());
			ps.setString(3, u.getSex());
			ps.setInt(4, u.getAge());
			ps.setString(5, u.getBirth());
			ps.setInt(6, u.getFlag());
			index = ps.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return index;
	}

	@Override
	public int userChangePwdDao(String newPwd, int uid) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement ps = null;
		int index = -1;
		User u = null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			//获取链接
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/2020?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "19971003qaz");
			//创建SQl命令
			String sql = "update t_user set pwd=? where uid=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, newPwd);
			ps.setInt(2, uid);
			index = ps.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return index;
	}

}
